package com.yunshang.shoppingmall.common;

import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/*上传类*/
public class UploadImgs {

    public static Map<String,Object> uploads(MultipartFile file){
        String imagePath="";
        Map<String,Object> map=new HashMap<>();
        String filename=file.getOriginalFilename();
        String uploadFileName=null;
        uploadFileName=System.currentTimeMillis()+filename.substring(filename.lastIndexOf("."));
//        String path="E:/images/uploads/";
        String path="/usr/local/mall_uploads/";
        File uploadPath=new File(path);
        if(!uploadPath.exists()){
            uploadPath.mkdirs();
        }

        File uploadFile=new File(uploadPath,uploadFileName);//服务器

        try {
            file.transferTo(uploadFile);
        } catch (IOException e) {
            e.printStackTrace();
        }

        //图片相对路径
        imagePath="/"+uploadFileName;
        map.put("imagePath",imagePath);
        return map ;
    }

}
